package vn.edu.fpt.taskmanager;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import vn.edu.fpt.taskmanager.tasks.Task;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * A class contains code for AddTask Screen.
 * @author GaSjeuThjt
 */
public class ViewTaskActivity extends TaskManagerActivity {

	private Logger logger = Logger.getLogger(ViewTaskActivity.class);
	
	private Button btnNewTask;
	private TextView txtTaskList;

	/**
	 * Set content view, get reference to controls (buttons, textviews, edittexts ...) and add necessary listeners.
	 * @author GaSjeuThjt
	 */
	private void setUpView() {
		
		setContentView(R.layout.activity_viewtask);
		
		btnNewTask = (Button) findViewById(R.id.newtask_button);
		btnNewTask.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ViewTaskActivity.this, AddTaskActivity.class);
				startActivity(intent);
			}
		});
		
		txtTaskList = (TextView) findViewById(R.id.tasklist_textview);
	}

	/**
	 * Get the ArrayList which contains all the tasks, sequentially append each element to a StringBuilder 
	 * then set the text property of the task list TextView to be the StringBuilder. 
	 * @author GaSjeuThjt
	 */
	private void showTask() {
		ArrayList<Task> taskList = getTaskManagerApplication().getTaskList();
		StringBuilder sb = new StringBuilder();
		for (Task t : taskList) {
			sb.append(String.format("* %s\n", t.toString()));
		}
		txtTaskList.setText(sb.toString());
	}
	
	/**
	 * Setup the view (screen).
	 * @author GaSjeuThjt
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setUpView();
		
		// Logging
		logger.info("View is setup for ViewTaskActivity");
	}

	/**
	 * @author GaSjeuThjt
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.view_task, menu);
		return true;
	}
	
	/**
	 * Show the added task on the screen.
	 * @author GaSjeuThjt
	 */
	@Override
	protected void onResume() {
		super.onResume();
		showTask();
		
		// Logging
		logger.info("Screen refreshed (showTask() method get called)");
	}

}
